---@class UIGuildBattleTeam : UIBase
UIGuildBattleTeam = UIBase.New(PanelNames.UIGuildBattleTeam)
local t = UIGuildBattleTeam

function t.Init(_fightType, _formationType)
    t.fightType = _fightType or EnumConst.FightTypeConst.DarkStreet;
    t.formationType = _formationType or EnumConst.FormationType.DarkStreet;
    UIBase.Init(t)
end

function t:OnCreate()
    self.teamItemList = {}
    for i = 1,6 do
        local item = {}
        local root = self:GetChild("Btn_HeroPos"..i)
        item.root = root
        item.hero = self:GetChild("hero",root)
        item.ctrlState = self:GetController("state",root)
        self:RegisterOnClick(item.root,self.ClickTeamItem,self,i)
        table.insert(self.teamItemList,item)
    end
    self.heroItemList = self:GetList("List_HeroSelect")
    self.btnEnter = self:GetChild("Btn_Enter")
    self.btnClose = self:GetChild("back")
    self.ctrlTab = self:GetController("Tab")
    --self.typeController = self:GetController("type")
    self.supSkillItem = {}
    self.supSkillItem.btn = self:GetChild("Com_SupSkill")
    self.supSkillItem.redDots = self:GetChild("Com_SupSkill/n11")
    self.supSkillItem.icon = self:GetChild("Com_SupSkill/Com_SupSkillMask/Icon")
    self.supSkillItem.ctrlAdd = self:GetController("Add",self.supSkillItem.btn)

    self:SetDynamicList(self.heroItemList,self.SelectItemRender,self)
    self:RegisterOnChanged(self.ctrlTab,self.TabChanged,self)
    self:RegisterOnClick(self.btnEnter,self.ClickEnter,self)
    self:RegisterOnClick(self.supSkillItem.btn,self.ClickSupSkill,self)
    self:RegisterOnClick(self.btnClose,self.ClickClose,self)

    self:RegisterEvent(Config.EventType.AssistantSkill, self.RefreshSupSkill);
    self:RegisterEvent(Config.EventType.DarkStreetFormationUpdate,self.FormationUpdateMsg)
    self:RegisterEvent(Config.EventType.Fresh_UnionWarFormations,self.FormationUpdateMsg)

    self.selectPage = 0
    self.selectList = {}
    self.stateTb = {}
    self.isClick = false
    self:RefreshSupSkill(0)
    --[[
    if self.formationType == EnumConst.FormationType.GuildBattle then
        self.typeController.selectedIndex = 1;
    else
        self.typeController.selectedIndex = 0;
    end
    --]]
    local f = logicMgr.FormationManager.GetFormation(self.formationType)
    if not f then
        if self.formationType == EnumConst.FormationType.DarkStreet then
            DarkStreetHandler.ReqDarkAlleyFormations();
        elseif self.formationType == EnumConst.FormationType.GuildBattle then
            unionWarHandler.reqUnionWarFormationsInfo();
        end
    end
end

function t:OnEnable()

    if self.ctrlTab.selectedIndex ~= 0 then
        self.ctrlTab.selectedIndex = 0
    else
        self:Refresh(true)
    end
end
function t:OnDisable()
end
function t:OnDestroy()
    self.selectList = nil
    self.heroList = nil
    self.stateTb = nil
end
function t:Refresh(useFormation)
    self.heroList = HeroManager.getOwnerHeroListNoClone(self.selectPage)
    table.sort(self.heroList,HeroManager.sortHero)
    if useFormation then
        local f = logicMgr.FormationManager.GetFormation(self.formationType)
        if f then
            for k,v in pairs(f) do
                if v ~= "0" and v ~= "" then
                    self.selectList[k] = HeroManager.getHeroDatabyPartnerId(v,true)
                end
            end
        end
    end
    self:RefreshList()
    self:RefreshTeam()
end
function t:RefreshList()
    self.heroItemList.numItems = #self.heroList
end
---@param go GComponent
function t:SelectItemRender(index,go)
    local data = self.heroList[index]
    ---@type HeadRectItem
    local item = CtrlManager.GetCtrl(PanelNames.HeadRectItem)
    item.initData(go,data)
    self:UpdateState(data.partnerId,item,go)
    go.onClick:Clear()
    go.onClick:Add(function()
        self:ClickHero(index,item,go)
    end)
end
function t:UpdateState(partnerId,item,go)
    if self:HasSelect(partnerId) then
        self.stateTb.state = EnumConst.HeroHeadStateEnum.BATTLE
    else
        self.stateTb.state = EnumConst.GuildStateType.NONE
    end
    item.updateState(go,self.stateTb)
end
function t:HasSelect(partnerId)
    for k,v in pairs(self.selectList) do
        if v.partnerId == partnerId then
            return true
        end
    end
    return false
end
function t:ClickHero(index,item,go)
    local data = self.heroList[index]
    local find = false
    for k,v in pairs(self.selectList) do
        if v.partnerId == data.partnerId then
            find = true
            self.selectList[k] = nil
            break
        end
    end
    if not find then
        if table.getn(self.selectList) >= 6 then
            UISysTips.AddMsg(getLanguage("DarkLaneTipes_8")) --上阵英雄已达到上限
            return
        end
        for i = 1,6 do
            if not self.selectList[i] then
                self.selectList[i] = data
                break
            end
        end
    end
    self:UpdateState(data.partnerId,item,go)
    self:RefreshTeam()
end
function t:RefreshTeam()
    for k,item in pairs(self.teamItemList) do
        local data = self.selectList[k]
        if data then
            item.ctrlState.selectedIndex = 1
            ---@type HeadRectBItem
            local ctrl = CtrlManager.GetCtrl(PanelNames.HeadRectBItem)
            ctrl.initData(item.hero,data)
            ctrl.ShowOwnCount(item.hero,false)
        else
            item.ctrlState.selectedIndex = 0
        end
    end
end
function t:RefreshSupSkill()
    local id = logicMgr.SkillManager.GetSelectSkillId()
    local skillData = G.dataTable["t_skillshow"][tostring(id)];
    local isRedDot = false;
    if skillData then
        self.supSkillItem.ctrlAdd.selectedIndex = 1
        self.supSkillItem.icon.url = "ui://SkillAtlas/" .. skillData.f_SkillIcon;
    else
        self.supSkillItem.ctrlAdd.selectedIndex = 0
        isRedDot = true
    end
    if not FuncOpenManager.isOpen(Config.FuncOpenType.SupSkill, false) then
        isRedDot = false;
    end
    self.supSkillItem.redDots.visible = isRedDot;
end
function t:ClickTeamItem(index)
    local item = self.selectList[index]
    if item then
        self.selectList[index] = nil
        self:RefreshList()
        self:RefreshTeam()
    end
end
function t:TabChanged()
    self.selectPage = self.ctrlTab.selectedIndex
    self:Refresh()
end
function t:DelayClick()
    self.isClick = false
end
function t:ClickEnter()
    if self.isClick then
        return
    end
    self.isClick = true
    self:TimeCall(self.DelayClick,2000)
    if table.getn(self.selectList) == 0 then
        UISysTips.AddMsg("至少需要上阵一名英雄")
        return
    end
    local formation = {}
    local flag = false;
    for i = 1,6 do
        local d = self.selectList[i]
        if d then
            formation[i] = d.partnerId
        else
            formation[i] = "0"
            flag = true;
        end
    end
    print("ClickEnter")
    if flag then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
        pop.Init("54", function(b)
            if b then
                self:ReqSetFormations(formation)
            end
        end);
    else
        self:ReqSetFormations(formation)
    end
end

function t:ReqSetFormations(formation)
    logicMgr.FormationManager.SetFormation(self.formationType,formation)
    if self.formationType == EnumConst.FormationType.DarkStreet then
        DarkStreetHandler.ReqDarkEntry(formation)
    elseif self.formationType == EnumConst.FormationType.GuildBattle then
        unionWarHandler.reqUnionWarFormationsSet(formation)
        UISysTips.AddMsg("设置阵容成功")
        self:ClickClose()
    end
end
--支援技
function t:ClickSupSkill()
    if FuncOpenManager.isOpen(Config.FuncOpenType.SupSkill, true, self.btnSupSkill) then
        showUI(PanelNames.UISupSelect, nil, self.fightType,EnumConst.FormationType.GuildBoss);
    end
end
function t:ClickClose()
    closeUI(PanelNames.UIGuildBattleTeam)
end
function t:FormationUpdateMsg()
    self:Refresh(true)
end
return t